﻿'
' Copyright (c) Microsoft Corporation. All rights reserved.
'
' This source code is subject to terms and conditions of the Microsoft Public
' License (Ms-PL). A copy of the license can be found in the license.htm file
' included in this distribution.
'
' You must not remove this notice, or any other, from this software.
'
Public Class XhtmlHelper

    Public Shared ReadOnly ValidationInputCssClassName As String = "input-validation-error"
    Public Shared ReadOnly ValidationMessageCssClassName As String = "field-validation-error"
    Public Shared ReadOnly ValidationSummaryCssClassName As String = "validation-summary-errors"

    Private _viewContext As ViewContext
    Private _viewDataContainer As IViewDataContainer
    Private _urlHelper As UrlHelper
    Private _routeData As RouteData

    Sub New(ByVal viewContext As ViewContext, ByVal viewDataContainer As IViewDataContainer)
        If viewContext Is Nothing Then
            Throw New ArgumentNullException("viewContext")
        End If
        If viewDataContainer Is Nothing Then
            Throw New ArgumentNullException("viewDataContainer")
        End If
        _viewContext = viewContext
        _viewDataContainer = viewDataContainer
        _routeData = viewContext.RouteData
        _urlHelper = New UrlHelper(viewContext.RequestContext)

    End Sub

    'Public Function RenderPartial(ByVal view As VbView) As XElement
    '    Return view.Render(_viewContext)
    'End Function

    Public ReadOnly Property RouteData() As RouteData
        Get
            Return _routeData
        End Get
    End Property

    Public ReadOnly Property ViewDataContainer() As IViewDataContainer
        Get
            Return _viewDataContainer
        End Get
    End Property

    Public ReadOnly Property ViewData() As ViewDataDictionary
        Get
            Return _viewDataContainer.ViewData
        End Get
    End Property

    Public ReadOnly Property ViewContext() As ViewContext
        Get
            Return _viewContext
        End Get
    End Property

    Friend Overridable Function RenderPartialInternal(ByVal view As VbView, ByVal vData As ViewDataDictionary, ByVal model As Object) As XElement

        Dim newViewData As ViewDataDictionary = Nothing

        If model Is Nothing Then
            If vData Is Nothing Then
                newViewData = New ViewDataDictionary(ViewData)
            Else
                newViewData = New ViewDataDictionary(vData)
            End If
        Else
            If vData Is Nothing Then
                newViewData = New ViewDataDictionary(model)
            Else
                newViewData = New ViewDataDictionary(vData)
                newViewData.Model = model
            End If
        End If

        Dim newViewContext As New ViewContext(_viewContext, _viewContext.View, newViewData, _viewContext.TempData)
        Return view.Render(newViewContext)
    End Function

    Public Function ActionLink(ByVal text As String, ByVal actionName As String) As XElement
        Return ActionLink(text, actionName, _routeData.GetRequiredString("controller"))
    End Function

    Public Function ActionLink(ByVal text As String, ByVal actionName As String, ByVal controllerName As String) As XElement
        Dim url = _urlHelper.Action(actionName, controllerName)
        Return <a href=<%= url %>><%= text %></a>
    End Function

    Public Function ActionLink(ByVal text As String, ByVal actionName As String, ByVal routeValues As Object) As XElement
        Return ActionLink(text, actionName, _routeData.GetRequiredString("controller"), routeValues)
    End Function

    Public Function ActionLink(ByVal text As String, ByVal actionName As String, ByVal controllerName As String, ByVal routeValues As Object) As XElement
        Dim url = _urlHelper.Action(actionName, controllerName, routeValues)
        Return <a href=<%= url %>><%= text %></a>
    End Function

    Public Function Form(ByVal innerContent As XElement) As XElement
        Return Form(_viewContext.HttpContext.Request.Url.ToString(), innerContent)
    End Function

    Public Function Form(ByVal actionName As String, ByVal controllerName As String, ByVal innerContent As XElement) As XElement
        Dim actionUrl = _urlHelper.Action(actionName, controllerName)
        Return Form(actionUrl, innerContent)
    End Function

    Public Function Form(ByVal actionUrl As String, ByVal innerContent As XElement) As XElement
        Return <form action=<%= actionUrl %> method="POST">
                   <%= innerContent %>
               </form>
    End Function

    'Private Function InputField(ByVal type As String, ByVal id As String) As XElement
    '    Dim elem = <input id=<%= id %> name=<%= id %> type=<%= type %> value=""/>

    '    Dim modelValue As ModelState = Nothing
    '    If _viewData.ModelState.TryGetValue(id, modelValue) Then
    '        elem.SetAttributeValue("value", modelValue.Value.AttemptedValue)

    '        If modelValue.Errors.Count > 0 Then
    '            elem.SetAttributeValue("class", "input-validation-error")
    '        End If
    '    End If

    '    Return elem
    'End Function

    Public Function Password(ByVal id As String) As XElement
        Return <input id=<%= id %> name=<%= id %> type="password"/> ' InputField("password", id)
    End Function

    'Encode function is reduntant because XML Literals automatically encode strings

End Class
